package com.example.floristshop;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.floristshop.database.User;

import org.litepal.LitePal;

import java.util.List;

public class LoginActivity extends AppCompatActivity {

    private EditText name_et, password_et;
    private Button login_button, register_button;
    private String name, password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        name_et = (EditText)findViewById(R.id.login_name_input);
        password_et = (EditText)findViewById(R.id.login_password_input);
        login_button = (Button)findViewById(R.id.login_button);
        register_button = (Button)findViewById(R.id.register_button);

        name_et.setText(getIntent().getStringExtra("name"));
        password_et.setText(getIntent().getStringExtra("password"));

        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name = name_et.getText().toString();
                password = password_et.getText().toString();

                List<User> users = LitePal.findAll(User.class);
                User user;
                if(users.size()==0){
                    Toast.makeText(LoginActivity.this, "该用户名尚未注册", Toast.LENGTH_SHORT).show();
                }
                for(int i=0; i<users.size(); i++){
                    user = users.get(i);
                    if(user.getName().equals(name)){
                        if(user.getPassword().equals(password)){
                            SharedPreferences.Editor editor = getSharedPreferences("user", MODE_PRIVATE).edit();
                            editor.putBoolean("is_login", true);
                            editor.putLong("user_id", user.getUser_id());
                            editor.putString("user_name", user.getName());
                            editor.putInt("user_image_id", user.getImage_id());
                            editor.apply();
                            Toast.makeText(LoginActivity.this, "用户「"+name+"」登录成功!", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }else{
                            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                            password_et.setText("");
                        }
                        break;
                    }
                    if(i==users.size()-1){
                        Toast.makeText(LoginActivity.this, "该用户名尚未注册", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        register_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
}